package cn.hg.solon.youcan.system.provider;

import static cn.hg.solon.youcan.system.entity.table.SysNoticeTableDef.SYS_NOTICE;

import java.util.Map;

import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.noear.solon.annotation.Component;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;

import cn.hg.solon.youcan.system.entity.SysNotice;
import cn.hg.solon.youcan.system.mapper.SysNoticeMapper;
import cn.hg.solon.youcan.system.service.SysNoticeService;

/**
 * @author 胡高
 */
@Component
public class SysNoticeProvider extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    @Override
    public Page<SysNotice> pageByMap(Page<SysNotice> page, Map<String, Object> map) {
        String word = (String)map.get("word");
        String type = (String)map.get("type");
        String status = (String)map.get("status");
        String sortField = (String)map.get("sortField");
        String sortType = (String)map.get("sortType");
        QueryWrapper query = QueryWrapper.create()
            .where(SYS_NOTICE.TYPE.eq(type).when(StrValidator.isNotBlank(type))
                .and(SYS_NOTICE.STATUS.eq(status).when(StrValidator.isNotBlank(status)))
                .and(SYS_NOTICE.TITLE.like(word).when(StrValidator.isNotBlank(word))
                    .or(SYS_NOTICE.CONTENT.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_NOTICE.REMARK.like(word).when(StrValidator.isNotBlank(word)))
                )
            );

        if (StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType)) {
            query.orderBy(new QueryOrderBy(new QueryColumn(CharSequenceUtil.toUnderlineCase(sortField)), sortType));
        }

        return this.page(page, query);
    }

    @Override
    public boolean save(SysNotice bean) {
        return super.save(bean);
    }

    @Override
    public boolean update(SysNotice bean) {
        return this.updateById(bean);
    }

}
